package cc.telecomdigital.tdstock.activity.groups.monitor.util;

import java.util.HashMap;

import android.content.Context;
import android.text.TextUtils;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.model.ForeignExchangeInfo;
import cc.telecomdigital.tdstock.model.IndexFuturesInfo;
import cc.telecomdigital.tdstock.model.IndexInfo;
import cc.telecomdigital.tdstock.model.IntlMetalInfo;
import cc.telecomdigital.tdstock.model.OilInfo;
import cc.telecomdigital.tdstock.model.StockInfo;
import cc.telecomdigital.tdstock.model.WorldFuturesInfo;
import cc.telecomdigital.tdstock.model.WorldIndexDelayInfo;
import cc.telecomdigital.tdstock.model.WorldIndexInfo;

/**
 * 
 * Project：tdstock3 Package：cc.telecomdigital.tdstock Class：BookMarkUtil
 * Description： Creater：hunter [chengpanggang@126.com//hunter@mango.cc]
 * CreateTime：Date：Nov 11, 2011 Time：11:54:31 AM Modifier ：hunter
 * [chengpanggang@126.com//hunter@mango.cc] Modify time： Date：Nov 11, 2011
 * Time：11:54:31 AM Modify Remarks： adapters［］成员后带＠的是在监察表中有的项目
 * 
 * @version
 * 
 */
public class BookMarkUtil
{

	private static final String	TAG	= BookMarkUtil.class.getSimpleName();

	public interface Filter
	{
		public ItemRecord addFilter(String itemCode);
	}

	public static HashMap<String, ItemRecord>	filterList							= new HashMap<String, ItemRecord>();

	private static final String[]				INDEX_FUTURES_ITEMS						= {
			"HSI:1", "HSI:2", "HHI:1", "HHI:2"									};

	final static String[]					IndexInfo_ADAPTER_items					= {
			"HSI", "HSCEI", "HSCCI", "GEM", "HSI-FIN", "HSI-UTL", "HSI-PRO",
			"HSI-CnI"														};
	final static int[]						IndexInfo_ADAPTER_itemNames				= {
			R.string.index_hsi_label, R.string.index_hscei_label,
			R.string.index_rc_label, R.string.index_ep_label,
			R.string.index_finance_label, R.string.index_public_label,
			R.string.index_estate_label, R.string.index_ic_label						};

	/*
	 * final static String[] IndexFuturesInfo_adapter_realtime_items= { "HSI:1",
	 * "HSI:2", "HHI:1", "HHI:2"};
	 */
	final static int[]						IndexFuturesInfo_adapter_realtime_itemNames	= {
			R.string.index_futures_hsi_later_label,
			R.string.index_futures_hsi_later_label,
			R.string.index_hscei_later_label,
			R.string.index_hscei_later_label										};

	final String[]							IndexFuturesInfo_adapter_delay_items		= {
			"HSI:1", "HSI:2", "HHI:1", "HHI:2"									};
	final int[]							IndexFuturesInfo_adapter_delay_itemNames	= {
			R.string.index_futures_hsi_morelater_label,
			R.string.index_futures_hsi_morelater_label,
			R.string.index_hscei_morelater_label,
			R.string.index_hscei_morelater_label									};

	final static String[]					IntlMetalInfo_ADAPTER_items				= {
			"XAU", "GCV9", "HKGG", "XAG", "COPPER", "NICKEL", "ALUM", "ZINC"			};
	final static int[]						IntlMetalInfo_ADAPTER_itemNames			= {
			R.string.more_metal_item_label_london,
			R.string.more_metal_item_label_goldfutures,
			R.string.more_metal_item_label_hkgold,
			R.string.more_metal_item_label_silver									};

	final static String[]					WorldIndexDelayInfo_ADAPTER_items			= {
			"SZEA", "SZEB"													};
	final static int[]						WorldIndexDelayInfo_ADAPTER_itemNames		= {
			R.string.index_sza_label, R.string.index_szb_label						};

	final static String[]					WorldIndexInfo_ADAPTER_items				= {
			/* "SSE1", */"N225", "FTSE", "GDAX", "DJI", /*
														 * "SS300", "SSE1", "SSEA",
														 * "SSEB",
														 */"N225",
			"KOSPI", "STI", "TWII", "AORD", "NZTOP",
			/* "BOMBAY", */"THAI", "JKSE", "KLSE", "PHSI", "DJI", "NDX",
			"SP500",/* 隐藏印度SEN index=10 */
			"FTSE", "CAC40", "GDAX"												};
	final static int[]						WorldIndexInfo_ADAPTER_itemNames			= {
			R.string.index_n225_label, R.string.index_ftse_label,
			R.string.index_gdax_label, R.string.index_dji_label,
			R.string.index_n225_label_2, R.string.index_kospi_label,
			R.string.index_sti_label, R.string.index_twii_label,
			R.string.index_aord_label, R.string.index_nztop_label,
			R.string.index_thai_label, R.string.index_jkse_label,
			R.string.index_klse_label, R.string.index_phsi_label,
			R.string.index_dji_label_2, R.string.index_ndx_label,
			R.string.index_sp500_label, R.string.index_ftse_label_2,
			R.string.index_cac40_label, R.string.index_gdax_label_2,					};

	final static String[]					ForeignExchangeInfo_ADAPTER_items			= {
			"EUR", "JPY", "GBP", "AUD", "CHF", "NZD", "CAD",
			"HKD", // SUBSCRIBE_ITEMS_DOLLAR
			"EURGBP", "EURCHF", "GBPJPY", "AUDJPY", "EURJPY",
			"CHFJPY", // SUBSCRIBE_ITEMS_CROSSRATE
			"EURHKD", "JPYHKD", "GBPHKD", "AUDHKD", "CHFHKD", "NZDHKD",
			"CADHKD" // SUBSCRIBE_ITEMS_HKDOLLAR
																			};
	final static String[]					ForeignExchangeInfo_ADAPTER_itemNames		= {
			"歐元/美元", "美元/日圓", "英鎊/美元", "澳元/美元", "美元/瑞郎", "紐元/美元", "美元/加元",
			"美元/港元", "歐元/英鎊", "歐元/瑞郎", "英鎊/日圓", "澳元/日圓", "歐元/日圓", "瑞郎/日圓",
			"歐元/港元", "日圓/港元", "英鎊/港元", "澳元/港元", "瑞郎/港元", "紐元/港元", "加元/港元"				};

	final static String[]					OilInfo_ADAPTER_items					= { "LCOIL" };

	final static String[]					OilInfo_ADAPTER_itemNames				= { "紐約原油" };

	final String[]							WorldFuturesInfo_ADAPTER_items			= {
			"SSI:1", "SSI:2", "DJI:1"											};
	final String[]							WorldFuturesInfo_ADAPTER_itemNames			= {
			"日經%s月", "日經%s月", "道指%s月"											};

	final static String[]					WORLD_INDEX_ITEMS_DELAY_AND_REALTIME		= {
			"SSE1", "SS300", "SSE1", "SSEA", "SSEB"								};
	final int[]							WORLD_INDEX_DELAYREALTIME_LABEL_IDS		= {
			R.string.index_sse1_label, R.string.index_ss300_label,
			R.string.index_sse1_label, R.string.index_ssea_label,
			R.string.index_sseb_label											};
	/*	*//**
			* 带＠的是在监察表中有的项目
			*/
	/*
	 * private static final String adapters[] = { ForeignExchangeInfo.ADAPTER,
	 * // 0.外汇 // "FX" // @ //
	 * ==================================================
	 * =============================== FuturesDelayInfo.ADAPTER, // 1. 延迟期货
	 * " FUTURESDELAY" //
	 * ========================================================
	 * ========================= GoldFuturesInfo.ADAPTER, // 2. 金属期货 "FX" //
	 * ====
	 * ======================================================================
	 * ======= HolidayInfo.ADAPTER, // 3. 假期 "HOLIDAY" //
	 * ========================
	 * =========================================================
	 * IndexFuturesInfo.adapter_realtime, // 4. 实时指数期货 "FUTURES" @ //
	 * ============
	 * =====================================================================
	 * IndexFuturesInfo.adapter_delay, // 5. 延迟指数期货 "FUTURESDELAY" @ //
	 * ==========
	 * =======================================================================
	 * IndexInfo.ADAPTER, // 6. 指数 "TELETEXT" @ //
	 * ==============================
	 * ===================================================
	 * IntlMetalInfo.ADAPTER, // 7. 国际金属 "FX" @ //
	 * ==============================
	 * =================================================== OilInfo.ADAPTER, //
	 * 8. 油价 "OIL" @ //
	 * ==========================================================
	 * ======================= StockInfo.ADAPTER, // 9. 股票 "TELETEXT" @ //
	 * ======
	 * ====================================================================
	 * ======= TimestampInfo.ADAPTER, // 10.时间戳 "TELETEXT" //
	 * ====================
	 * =============================================================
	 * Top20StockInfo.ADAPTER, // 11. 股票二十大排名 "STOCKLIST" //
	 * ====================
	 * =============================================================
	 * WorldFuturesInfo.ADAPTER, // 12. 国际期货 "WORLDFUTURES" @ //
	 * ================
	 * =================================================================
	 * WorldIndexDelayInfo.ADAPTER, // 13.延迟 国际指数 "WORLDINDEXDELAY" @ //
	 * ========
	 * =========================================================================
	 * WorldIndexInfo.ADAPTER // 14. 国际指数 "WORLDINDEX" @ };
	 */
	private static ITDLApplication			app;
	private static BookMarkUtil				t									= new BookMarkUtil();

	private BookMarkUtil()
	{
		/*
		 * Filter first = new Filter() { public ItemRecord addFilter(String
		 * itemCode) { if(itemCode.equals("1")) { return new ItemRecord("1",
		 * "长江实业", StockInfo.ADAPTER); } return null; } };
		 * 
		 * 
		 * _filterList.put("1", first.addFilter("1"));
		 */

	}

	private static void addForeignExchangeFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = ForeignExchangeInfo_ADAPTER_items.length;

				for (index = 0; index < count; index++)
				{
					if (ForeignExchangeInfo_ADAPTER_items[index]
							.equals(itemCode))
					{
						return new ItemRecord(
								itemCode,
								ForeignExchangeInfo_ADAPTER_itemNames[index],
								ForeignExchangeInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		for (String item : ForeignExchangeInfo_ADAPTER_items)
			filterList.put(item, filter.addFilter(item));

	}

	private static void addMetalFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = IntlMetalInfo_ADAPTER_itemNames.length;

				for (index = 0; index < count; index++)
				{
					if (IntlMetalInfo_ADAPTER_items[index]
							.equals(itemCode))
					{
						return new ItemRecord(
								itemCode,
								app.getResources()
										.getString(
												IntlMetalInfo_ADAPTER_itemNames[index]),
								IntlMetalInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		int count = IntlMetalInfo_ADAPTER_itemNames.length;
		for (int index = 0; index < count; index++)
			filterList.put(IntlMetalInfo_ADAPTER_items[index],
					filter.addFilter(IntlMetalInfo_ADAPTER_items[index]));

	}

	// including the cop(the only difference is that showing different
	// column(one is cop and the other is last)
	private static void addIndexFuturesFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = INDEX_FUTURES_ITEMS.length;
				boolean isDelayMode = (app.getDataMode() == CommonDefn.DataMode.Delay);
				for (index = 0; index < count; index++)
				{
					if (INDEX_FUTURES_ITEMS[index].equals(itemCode))
					{
						return new ItemRecord(
								itemCode,
								itemCode.matches("HSI:[0-9]") ? "恆指%s月"
										: "國企%s月",
								isDelayMode ? IndexFuturesInfo.adapter_delay
										: IndexFuturesInfo.adapter_realtime);
					}
				}
				return null;
			}
		};

		for (String item : INDEX_FUTURES_ITEMS)
			filterList.put(item, filter.addFilter(item));

	}

	private static void addWorldIndexFuturesFilter()
	{

		final String[] WORLD_FUTURES_ITEMS = { "SSI:1", "SSI:2", "DJI:1" };
		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = WORLD_FUTURES_ITEMS.length;
				for (index = 0; index < count; index++)
				{
					if (WORLD_FUTURES_ITEMS[index].equals(itemCode))
					{
						return new ItemRecord(itemCode,
								itemCode.matches("SSI:[0-9]") ? "日經%s月"
										: "道指%s月",
								WorldFuturesInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		for (String item : WORLD_FUTURES_ITEMS)
			filterList.put(item, filter.addFilter(item));

	}

	private static void addIndex4ShenZhenAndShangHaiFilter()
	{

		final int[] WORLD_INDEX_DELAYREALTIME_LABEL_IDS = {
				R.string.index_sse1_label, R.string.index_ss300_label,
				R.string.index_sse1_label, R.string.index_ssea_label,
				R.string.index_sseb_label };

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = WORLD_INDEX_ITEMS_DELAY_AND_REALTIME.length;
				boolean isDelayMode = (app.getDataMode() == CommonDefn.DataMode.Delay);
				TDStockLog.d("test", app.getDataMode() + "");
				for (index = 0; index < count; index++)
				{
					if (WORLD_INDEX_ITEMS_DELAY_AND_REALTIME[index]
							.equals(itemCode))
					{
						String label = app
								.getResources()
								.getString(
										WORLD_INDEX_DELAYREALTIME_LABEL_IDS[index])
								.replaceAll("[\n\r]", "")
								.replaceAll("(<br>)|(<BR>)", "")
								.replace("(延遲)", "");
						return new ItemRecord(
								itemCode,
								label,
								isDelayMode ? WorldIndexDelayInfo.ADAPTER
										: WorldIndexInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		for (String item : WORLD_INDEX_ITEMS_DELAY_AND_REALTIME)
			filterList.put(item, filter.addFilter(item));

	}

	private static void addOilFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = OilInfo_ADAPTER_items.length;

				for (index = 0; index < count; index++)
				{
					if (OilInfo_ADAPTER_items[index].equals(itemCode))
					{
						return new ItemRecord(itemCode,
								OilInfo_ADAPTER_itemNames[index],
								OilInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		int count = OilInfo_ADAPTER_items.length;
		for (int index = 0; index < count; index++)
			filterList.put(OilInfo_ADAPTER_items[index],
					filter.addFilter(OilInfo_ADAPTER_items[index]));

	}

	private static void addWorldIndexFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = WorldIndexInfo_ADAPTER_items.length;

				for (index = 0; index < count; index++)
				{
					if (WorldIndexInfo_ADAPTER_items[index]
							.equals(itemCode))
					{
						String label = app
								.getResources()
								.getString(
										WorldIndexInfo_ADAPTER_itemNames[index])
								.replaceAll("[\n\r]", "")
								.replaceAll("(<br>)|(<BR>)", "")
								.replace("(延遲)", "");
						return new ItemRecord(itemCode, label,
								WorldIndexInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		int count = WorldIndexInfo_ADAPTER_items.length;
		for (int index = 0; index < count; index++)
			filterList.put(WorldIndexInfo_ADAPTER_items[index],
					filter.addFilter(WorldIndexInfo_ADAPTER_items[index]));

	}

	private static void addIndex4HKFilter()
	{

		Filter filter = new Filter()
		{
			public ItemRecord addFilter(String itemCode)
			{
				int index = 0;
				int count = IndexInfo_ADAPTER_items.length;

				for (index = 0; index < count; index++)
				{
					if (IndexInfo_ADAPTER_items[index].equals(itemCode))
					{
						String label = app
								.getResources()
								.getString(
										IndexInfo_ADAPTER_itemNames[index])
								.replaceAll("[\n\r]", "")
								.replaceAll("(<br>)|(<BR>)", "")
								.replace("(延遲)", "");
						return new ItemRecord(itemCode, label,
								IndexInfo.ADAPTER);
					}
				}
				return null;
			}
		};

		int count = IndexInfo_ADAPTER_items.length;
		for (int index = 0; index < count; index++)
			filterList.put(IndexInfo_ADAPTER_items[index],
					filter.addFilter(IndexInfo_ADAPTER_items[index]));

	}

	private static void addWorddelayIndexFilter()
	{
		Filter filter = new Filter()
		{

			@Override
			public ItemRecord addFilter(String itemCode)
			{
				// TODO Auto-generated method stub
				int index = 0;
				int count = WorldIndexDelayInfo_ADAPTER_items.length;

				for (index = 0; index < count; index++)
				{
					if (WorldIndexDelayInfo_ADAPTER_items[index]
							.equals(itemCode))
					{
						String label = app
								.getResources()
								.getString(
										WorldIndexDelayInfo_ADAPTER_itemNames[index])
								.replaceAll("[\n\r]", "")
								.replaceAll("(<br>)|(<BR>)", "")
								.replace("(延遲)", "");
						return new ItemRecord(itemCode, label,
								WorldIndexDelayInfo.ADAPTER);
					}
				}
				return null;
			}

		};
		int count = WorldIndexDelayInfo_ADAPTER_items.length;
		for (int index = 0; index < count; index++)
			filterList.put(WorldIndexDelayInfo_ADAPTER_items[index], filter
					.addFilter(WorldIndexDelayInfo_ADAPTER_items[index]));
	}

	private static void addAllFilter()
	{
		addForeignExchangeFilter();
		addMetalFilter(); // only 4
		addIndexFuturesFilter(); // the adapter is different from each under
		// delay-mode and real-time
		addWorldIndexFuturesFilter();
		addIndex4ShenZhenAndShangHaiFilter(); // the adapter is different from
		// each under delay-mode and
		// real-time
		addOilFilter();
		addWorldIndexFilter();
		addIndex4HKFilter();
		addWorddelayIndexFilter();
	}

	public static BookMarkUtil getInstance()
	{
		return t;
	}

	public static BookMarkUtil getInstance(Context context)
	{
		app = ((ITDLApplication) context.getApplicationContext());
		addAllFilter();
		return t;
	}

	/*
	 * public static int findString(String[] strs,String rex){ for (int i = 0; i
	 * < strs.length; i++) { if(rex.trim().equalsIgnoreCase(strs[i])){ return i;
	 * } } return -1; }
	 * 
	 * public int getItemNameRString(String item){ int
	 * num=findString(IndexInfo_ADAPTER_items, item); if(num!=-1){ return
	 * IndexInfo_ADAPTER_itemNames[num]; }
	 * 
	 * num=findString(IndexFuturesInfo_adapter_realtime_items, item);
	 * if(num!=-1){ return IndexFuturesInfo_adapter_realtime_itemNames[num]; }
	 * num=findString(IndexFuturesInfo_adapter_delay_items, item); if(num!=-1){
	 * return IndexFuturesInfo_adapter_delay_itemNames[num]; }
	 * 
	 * 
	 * num=findString(IntlMetalInfo_ADAPTER_items, item); if(num!=-1){ return
	 * IntlMetalInfo_ADAPTER_itemNames[num]; }
	 * 
	 * 
	 * num=findString(WorldIndexDelayInfo_ADAPTER_items, item); if(num!=-1){
	 * return WorldIndexDelayInfo_ADAPTER_itemNames[num]; }
	 * num=findString(WorldIndexInfo_ADAPTER_items, item); if(num!=-1){ return
	 * WorldIndexInfo_ADAPTER_itemNames[num]; } return 0; }
	 * 
	 * public String getItemName(String item){ int num=getItemNameRString(item);
	 * if(num==0){ num=findString(ForeignExchangeInfo_ADAPTER_items, item);
	 * if(num!=-1){ return ForeignExchangeInfo_ADAPTER_itemNames[num]; }
	 * num=findString(OilInfo_ADAPTER_items, item); if(num!=-1){ return
	 * OilInfo_ADAPTER_itemNames[num]; }
	 * num=findString(WorldFuturesInfo_ADAPTER_items, item); if(num!=-1){ return
	 * WorldFuturesInfo_ADAPTER_itemNames[num]; } return ""; }else{ return
	 * context.getString(num).replaceAll("[\n\r]", "")
	 * .replaceAll("(<br>)|(<BR>)", "").replace("(延遲)", ""); } }
	 */

	/*
	 * public String getAdapter(String item){ String adapter="";
	 * if(TextUtils.isDigitsOnly(item)){ adapter=adapters[9]; }else
	 * if(findString(IndexInfo_ADAPTER_items, item)!=-1){ adapter=adapters[6];
	 * }else if(findString(IntlMetalInfo_ADAPTER_items, item)!=-1){
	 * adapter=adapters[7]; }else
	 * if(findString(ForeignExchangeInfo_ADAPTER_items, item)!=-1){
	 * adapter=adapters[0]; }else if(findString(OilInfo_ADAPTER_items,
	 * item)!=-1){ adapter=adapters[8]; }else
	 * if(findString(WorldFuturesInfo_ADAPTER_items, item)!=-1){
	 * adapter=adapters[9]; }else
	 * if(findString(IndexFuturesInfo_adapter_delay_items, item)!=-1){
	 * adapter=adapters[5]; }else
	 * if(findString(WorldIndexDelayInfo_ADAPTER_items, item)!=-1){
	 * adapter=adapters[13]; }else
	 * if(findString(IndexFuturesInfo_adapter_realtime_items, item)!=-1){
	 * adapter=adapters[4]; }else if(findString(WorldIndexInfo_ADAPTER_items,
	 * item)!=-1){ adapter=adapters[14]; } return adapter; }
	 */

	public ItemRecord getItemRecord(String itemCode)
	{
		ItemRecord result = null;
		if (TextUtils.isDigitsOnly(itemCode))
		{
			return new ItemRecord(itemCode, "", StockInfo.ADAPTER);
		}
		result = filterList.get(itemCode);
		TDStockLog.d(TAG, filterList.size() + "");
		TDStockLog.d(TAG, itemCode);
		return result;
	}

}
